package com.yangjie.user.service;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.yangjie.common.service.Service;
import com.yangjie.user.domain.User;
import com.yangjie.user.domain.UserDetailsAdapter;

@Component("userDetailsService")
public class UserDetailsServiceAdapter implements UserDetailsService {
	@Inject
	@Qualifier("userService")
	private UserService userService = null;
	
	
	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		System.out.println(".......................................");
		User user = ((UserService)userService).findUserByUsername(username);
		if(user == null) {
			throw new UsernameNotFoundException("can't find user:" + username);
		}
		return new UserDetailsAdapter(user);
	}

}
